#!/bin/bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: (c) 2015-2022 Advanced Micro Devices, Inc.
######################################################################

me=$(basename "$0")
err()  { echo >&2 "$*"; }
log()  { err "$me: $*"; }
fail() { log "FAILED: $*"; exit 1; }
try()  { "$@" || fail "$*"; }

######################################################################
# main()

tgz="$1"
try [ -f "$tgz" ]
dir=$(basename ${tgz})
dir=${dir%.tgz}
try tar xf "$tgz"
try [ -d "$dir" ]
dst=$(dirname ${tgz})

if [ -f "$dir/ChangeLog" ]; then
    try mv "$dir/ChangeLog" "$dst/$dir-ChangeLog.txt"
else
    err "No Changelog file found in Tarball"
fi

if [ -f "$dir/ReleaseNotes" ]; then
    try mv "$dir/ReleaseNotes" "$dst/$dir-ReleaseNotes.txt"
else
    err "No ReleaseNotes file found in Tarball"
fi

if [ -f "$dir/LICENSE" ]; then
    try mv "$dir/LICENSE" "$dst/$dir-LICENSE.txt"
else
    err "No LICENSE file found in Tarball"
fi

try rm -r "$dir"
